<template>
  <el-card>
    <div slot="header">
      <el-form
        :inline="true"
        ref="ruleForm"
        :model="ruleForm"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item label="工序编码" prop="process_code">
          <el-input
            v-model="ruleForm.process_code"
            placeholder="请输入工单编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="工序名称" prop="process_name">
          <el-input
            v-model="ruleForm.process_name"
            placeholder="请输入工单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="enable_flag">
          <el-select v-model="ruleForm.enable_flag" placeholder="请选择">
            <el-option value="">全部</el-option>
            <el-option value="是">已启用</el-option>
            <el-option value="否">未启用</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >查询</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          plain
          @click="add"
          >新增</el-button
        >
        <el-button size="small" type="danger" icon="el-icon-delete" plain
          >删除</el-button
        >
        <el-button
          style="float: right"
          icon="el-icon-refresh"
          size="small"
          @click="Refresh"
          circle
        ></el-button>
      </div>
    </div>

    <div>
      <el-table :data="tableData" style="width: 100%" max-height="600" v-loading="isloading">
        <el-table-column
          fixed
          prop="process_code"
          label="工序编号"
          width="auto"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="productionbtn(scope.row.process_id)"
              >{{ scope.row.process_code }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="process_name"
          label="工序名称"
          width="auto"
        >
        </el-table-column>

        <el-table-column
          prop="enable_flag"
          label="是否启用"
          width="auto"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.enable_flag === '是' ? 'primary' : 'info'"
              disable-transitions
            >
              <div>{{ scope.row.enable_flag }}</div>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="remark" label="备注" width="auto">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="auto" align="center">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row.process_id)" type="text" size="small"
              >修改</el-button
            >
            <el-button @click="del(scope.row.process_id)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :pageindex="PageIndex"
        :pagesize="PageSize"
        :total="total"
        @CurrentChange="CurrentChange"
        @SizeChange="SizeChange"
      />
    </div>


  <el-dialog
    class="box-dialog"
    :title="title"
    :visible.sync="dialogFormVisible"
    @close="closeDialog"
    :close-on-click-modal="false"
    custom-class="ss"
  >
    <ProcessDesignInput
      :ruleForms="ruleForms"
      :rules="rules"
      @save="save"
      @status="status"
      :islook="islook"
      :tableData="tableDatas" @deleteRow="deleteRow" :isupdate="isupdate" @update="update" :ruleFormss="ruleFormss" :ruless="ruless" :dialogFormVisible="dialogFormVisibles" :title="titles" @adds="adds" @statuss="statuss" @saves="saves"
    ></ProcessDesignInput>
  </el-dialog>
  </el-card>
</template>

<script>
import Pagination from "../../components/Pagination.vue";
import ProcessDesignInput from "../../components/ProcessDesignInput.vue";

export default {
  components: {
    ProcessDesignInput,
    Pagination
  },
  data() {
    return {
      isloading:false,
      isupdate:true,
      islook:true,
      title: "",
      titles: "",
      dialogFormVisible: false,
      dialogFormVisibles: false,
      PageIndex: 1,
      PageSize: 10,
      total: 0,
      ruleForm: {
        process_code: "",
        process_name: "",
        enable_flag: "",
      },
      ruleForms: {
        process_id: "",
        process_code: "",
        process_name: "",
        attention: "",
        enable_flag: "",
        remark: "",
        create_by: "",
        create_time: "",
        update_by: "",
        content_id: "",
        update_time: "",
      },
      ruleFormss:{
        content_id:"",
        process_id:"",
        order_num:1,
        content_text:"",
        device:"",
        material:"",
        remark:"",
        create_by:"",
      },
      tableData: [],
      tableDatas: [],

      rules: {
        process_code: [
          { required: true, message: "请输入工序编号", trigger: "blur" },
        ],
        process_name: [
          { required: true, message: "请输入工序名称", trigger: "blur" },
        ],
        enable_flag: [
          { required: true, message: "请选择是否启用", trigger: "blur" },
        ],
      },
      ruless: {
        order_num: [
          { required: true, message: "请输入顺序编号", trigger: "blur" },
        ],
        content_text: [
          { required: true, message: "请输入内容说明", trigger: "blur" },
        ]
      },
    };
  },
  methods: {
    statuss(e){
      this.dialogFormVisibles=false;
    },
    saves(e){
      this.ruleFormss.process_id=this.ruleForms.process_id;
      const loading = this.$loading();
      this.$http.postForm('/api/ProcessDesign/AddProcessContent',{pro:this.ruleFormss})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisibles=false;
                                this.GetProcess(this.ruleForms.process_id);
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
    },
    adds(e){
      this.titles="新增操作步骤";
      this.dialogFormVisibles=true;
      this.ruleFormss={};
    },
    update(e){
      const loading = this.$loading();
      this.$http.putForm('/api/ProcessDesign',{pro:this.ruleForms})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisible=false;
                                this.databind();
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
    },
    deleteRow(e){
      if(e != undefined){
                this.$confirm('此操作将永久删除数据, 是否继续?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                })
                .then(()=>{
                    const loading = this.$loading();
                    this.$http.delete('/api/ProcessDesign/DelProcessContent',{params:{Id:e}})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.GetProcess(this.ruleForms.process_id);
                            }
                            else{
                                this.ShowMsg(res.data.msg,'warning');
                            }
                        }).catch(err =>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
                }).catch(()=>{})
            }
    },
    productionbtn(e){
      this.islook=true;
      this.isupdate=false;
      this.title = "工序详情";
      this.GetProcess(e);
    },
    GetProcess(e){
      const loading = this.$loading();
this.$http
        .request({
          type: "get",
          url: "/api/ProcessDesign/GetProcess",
          params: {
            Id:e
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.ruleForms=res.data.data.data;
            this.tableDatas=res.data.data.list;
            this.dialogFormVisible=true;
          }
        })
        .catch((err) => {
          this.ShowMsg('服务器繁忙，请稍后重试','error');
        })
        .finally(() => {loading.close();});
    },
    CurrentChange(e) {
      this.PageIndex = e;
      this.databind();
    },
    SizeChange(e) {
      this.PageSize = e;

      //从第一页开始
      this.PageIndex = 1;
      this.databind();
    },
    closeDialog(){},
    status(e) {
      this.dialogFormVisible = false;
    },
    save() {
      const loading = this.$loading();
      this.$http.postForm('/api/ProcessDesign/AddProcess',{pro:this.ruleForms})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisible=false;
                                this.databind();
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
    },
    add() {
      this.dialogFormVisible = true;
      this.title = "新增工序";
      this.islook=false;
      this.isupdate=false;
      this.ruleForms={};
      this.tableDatas=[];
    },
    Refresh() {
      this.databind();
    },
    handleEdit(e) {
      this.isupdate=true;
      this.islook=true;
      this.title = "修改工序";
      this.GetProcess(e);
    },
    del(e) {
      if(e != undefined){
                this.$confirm('此操作将永久删除数据, 是否继续?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                })
                .then(()=>{
                    const loading = this.$loading();
                    this.$http.delete('/api/ProcessDesign',{params:{Id:e}})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.databind();
                            }
                            else{
                                this.ShowMsg(res.data.msg,'warning');
                            }
                        }).catch(err =>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
                }).catch(()=>{})
            }
    },
    submitForm(formName) {
      this.databind();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.databind();
    },
    databind() {
      this.isloading=true;
      this.$http
        .request({
          type: "get",
          url: "/api/ProcessDesign",
          params: {
            PageIndex: this.PageIndex,
            PageSize: this.PageSize,
            process_code: this.ruleForm.process_code,
            process_name: this.ruleForm.process_name,
            status: this.ruleForm.enable_flag,
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.total = res.data.count;
          }
        })
        .catch((err) => {
          this.ShowMsg('服务器繁忙，请稍后重试','error');
        })
        .finally(() => {
          this.isloading=false;
        });
    },
  },
  created() {
    this.databind();
  },
};
</script>

<style>
.ss {
  min-width: 800px;
}
</style>